package main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.SwingUtilities;

/**
 * Main class. Stores the contact list, and bridge between the contact
 * list data and the graphical user interface
 * 
 * @author Michael
 */
public class Main implements ActionListener {
    
    public static final String TITLE = "Contact List";

    private ContactList contactList;
    private MainMenu menu;
    private NewContactWindow newContactWindow;
    private SearchContactWindow searchWindow;

    /**
     * Handles all the buttons from the different windows MainWindow Add Contact
     * - opens a new NewContactWindow Search - opens a new SearchContactWindow
     * Print - opens a new PrintContactsWindow with the full string from the
     * contactList NewContactWindow Add -Checks if a contact was produced, then
     * adds or does nothing Cancel -Closes the window SearchWindow Search -
     * Grabs the information from the window and searches then opens a new
     * window with the results
     * 
     * @param arg0
     *            the actionevent used to determine which button was pressed
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
        String command = arg0.getActionCommand();
        
        
        // Buttons from MainWindow
        if (command == "main_add") {
            newContactWindow = new NewContactWindow(this);
            newContactWindow.setLocationRelativeTo(menu);
            newContactWindow.setModal(true);
            newContactWindow.setVisible(true);
        } else if (command == "main_search") {
            searchWindow = new SearchContactWindow(this);
            searchWindow.setLocationRelativeTo(menu);
            searchWindow.setModal(true);
            searchWindow.setVisible(true);
        } else if (command == "main_print") {
            contactList.sort();
            PrintContactsWindow print = new PrintContactsWindow(
                    contactList.toString());
            print.setLocationRelativeTo(menu);
            print.setModal(true);
            print.setVisible(true);
            
            
            // Buttons from Adding Contact Window
        } else if (command == "new_add") {
            Contact newContact = newContactWindow.getContact();
            if (newContact != null) {
                contactList.addContact(newContact);
                newContactWindow.dispose();
            }
        } else if (command == "new_cancel") {
            newContactWindow.dispose();
            
            
            // Search Button
        } else if (command == "search_search") {
            String searchBy = searchWindow.getSearchFilter();
            String search = searchWindow.getSearchKeywords();
            if(search.equals("")) {
                searchWindow.dispose();
                return;
            }
            String contactResults;
            System.out.println(searchBy);
            if (searchBy == "Zip Code") {
                System.out.println("Searching By zip");
                contactResults = contactList.searchByZip(search);
            } else if (searchBy == "Email") {
                contactResults = contactList.searchByEmail(search);
            } else {
                contactResults = contactList.searchByLastName(search);
            }
            searchWindow.dispose();
            PrintContactsWindow print = new PrintContactsWindow(
                    contactResults.toString());
            print.setLocationRelativeTo(menu);
            print.setModal(true);
            print.setVisible(true);
        }
    }

    /**
     * Reads previously saved data Starts the GUI
     */
    public void start() {
        contactList = Disk.read();
        menu = new MainMenu(this);
    }

    /**
     * Initializes a Main object and Starts
     * 
     * @param args
     */
    public static void main(String[] args) {
        Main main = new Main();
        main.start();

    }
}
